// 使用数组来模拟队列(Queue)
// 队列(Queue)的特点: 
// FIFO : First In First Out (先进先出)

let queue = [];

/** = = = = = == = = = = = 以数组的字符串形式的左侧为队列头，以右侧为队列尾 = = = = = == = = = = = */
// 在队列尾部添加元素
queue.push('刘备', '关羽', '张飞', '诸葛亮', '赵云');
console.log('queue: ', queue.toString());
console.log('- - - - - - - - - - - - - - - - - - - - -');

// 本来在 Array对象的原型上没有 first 函数，我们自己定义一个
Array.prototype.first = function() {
    return this[0];
}

// 检查队列头部元素(但不从队列中删除改元素)
let head = queue.first();
console.log('head: ', head);
console.log('queue: ', queue.toString());

// 获取并移除队列头部元素
head = queue.shift(); // Array.prototype.shift
console.log('head: ', head);
console.log('queue: ', queue.toString());

head = queue.shift();
console.log('head: ', head);
console.log('queue: ', queue.toString());

// 在队列尾部添加元素
queue.push('姜维');
console.log('queue: ', queue.toString());

head = queue.shift();
console.log('head: ', head);
console.log('queue: ', queue.toString());

console.log('- - - - - - - - - - - - - - - - - - - - -');

/** = = = = = = 以数组的字符串形式的【右侧为队列头】，以左侧为队列尾 = = = = = = */
console.log('queue: ', queue.toString());

// 在队列尾添加元素
queue.unshift('宋江'); // Array.prototype.unshift
console.log('queue: ', queue.toString());
queue.unshift('李逵');
console.log('queue: ', queue.toString());
queue.unshift('武松');
console.log('queue: ', queue.toString());

Array.prototype.last = function() {
    return this[this.length == 0 ? 0 : this.length - 1];
}

// 检查队列头部元素但不删除
head = queue.last();
console.log('head: ', head);
console.log('queue: ', queue.toString());

// 获取并移除队列头元素
head = queue.pop();
console.log('head: ', head);
console.log('queue: ', queue.toString());